package com.lry.web.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lry.web.video.entity.VideoUpdate;
import com.lry.web.video.entity.VideoVariable;
import com.lry.web.video.mapper.VideoVariableMapper;
import com.lry.web.video.processor.VideoVerifyProcessor;
import com.lry.web.video.service.VideoVariableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Spider;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VideoVariableServiceImpl implements VideoVariableService {
    @Autowired
    VideoVariableMapper videoVariableMapper;



    public int setVideoVariable(VideoVariable videoVariable) {
        int insert = videoVariableMapper.insert(videoVariable);
        return insert;
    }


    public List<VideoVariable> getVideoVariables() {
        return videoVariableMapper.selectList(null);
    }

    public int delete(Integer id) {
        return videoVariableMapper.deleteById(id);
    }

    public VideoVariable getVideoVariable(Integer id) {
        return videoVariableMapper.selectById(id);
    }
    //获取全部小说规则，分页
    public Map<String, Object> selectVideoVariables(Integer from, Integer size) {
        Page<VideoVariable> page = new Page<VideoVariable>(from,size);
        videoVariableMapper.selectPage(page, null);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("videoVariables",page.getRecords());
        map.put("pages",page.getPages());
        return map;
    }

    public int updateVideoVariable(VideoVariable videoVariable) {

        return videoVariableMapper.updateById(videoVariable);
    }

    public Map<String, Object> verifyVideoVariable(VideoVariable videoVariable, String name) throws Exception {
        Map<String,Object> map  = new HashMap<String,Object>();
        Spider.create(new VideoVerifyProcessor(videoVariable,map))
                //设置爬取数据的地址
                .addUrl(videoVariable.getSearchUrl()+name)
                //执行爬虫
                .thread(5)
                .run();
        return map;
    }



}
